var vm = new Vue({
    //vue对象节点,指向父盒子
    el:'#person',                                                                   
    //vue实例 的data属性
    data:function(){
    	var validatePass2 = function(rule, value, callback){
            if (value === '') {
              callback(new Error('请再次输入密码'));
            } else if (value !== vm.user.password) {
              callback(new Error('两次输入密码不一致!'));
            } else {
              callback();
            }
        };
        return{
        	loading:false,
        	tableData:[],
        	//分页条
        	pageSize:10,
        	currPage:1,
        	total:1,
        	pageA:true,
        	pageB:false,
        	pageC:false,
        	user:{},
        	rules:{ 
        		userCode: [{ required: true, message: '请输入用户名', trigger: 'blur' },
        			   	   { max: 15, message: '长度不能超过15个字符', trigger: 'blur' }],
			   	password: [{ required: true, message: '请输入用户名', trigger: 'blur' },
 			   	   { max: 15, message: '长度不能超过15个字符', trigger: 'blur' }],
 			   	rePassword: [{ required: true, message: '请再次输入密码', trigger: 'blur' },
 			   		{validator: validatePass2, trigger: 'blur'},
  			   	   { max: 15, message: '长度不能超过15个字符', trigger: 'blur' }],
  			   	tel: [{ required: true, message: '请输入手机号', trigger: 'blur' },
  			   	   
  			   		{ type: 'number', message: '必须为数字值'}],
  			   	email: [{ required: true, message: '请输入邮箱', trigger: 'blur' },
   			   	   		{type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change'}],
        	},
        	userTopicRules:{
        		tname: [{ required: true, message: '请填写主题', trigger: 'blur' }],
        	},
        	//帖子详情
        	newFlag:false,
  	      	userTopic:{
  	      	  "user":{},
	    	  "plate":{}
  	      	},
  	      	topicV:false,
  	      	topicData:{
	    	  "user":{},
	    	  "plate":{}
	        }
        };
    },
    //vue实例 的methods
    methods:{
       handleSizeChange:function(val) {
    	   vm.pageSize = val;
    	   vm.getTopicList();
       },
       handleCurrentChange:function(val) {
    	   vm.currPage = val;
    	   vm.getTopicList();
       },
       //格式化日期
       dateFormat:function(row,column){
    	   var date = row[column.property];
    	   if(date == undefined){
    		   return "";
    	   }
    	   return moment(date).format("YYYY-MM-DD HH:mm:ss");
       },
       statusFormat:function(row,column){
    	   var i = row[column.property];
    	   if(i == undefined){
    		   return "";
    	   }else if(i =='0' ){
    		  i="进行中"; 
    	   }
    	   else if(i =='1' ){
     		  i="完结"; 
     	   }
    	   else if(i =='2' ){
     		  i="禁言"; 
     	   }else if(i =='3' ){
     		  i="删除"; 
     	   }
    	   return i;
       },
       loadData:function(){
     	   vm.getUser();
     	   vm.getTopicList();
       },
       update:function(){
    	   vm.pageA = false;
    	   vm.pageB = true;
    	   vm.pageC = false;
    	   vm.getUser();  
       },
       updateTopic:function(tid){
    	   vm.userEditorEdit.setContent('');
    	   vm.pageA = false;
    	   vm.pageB = false;
    	   vm.pageC = true;
    	   vm.getRecord(tid);
       },
       getUser:function(){
    	   var url = "./getUserById.action";
    	   var params = {
    		   'user.id':window.parent.currUserId
    	   }
    	   pb.ajaxPost(url,params,function(r){
	       		if(r.code == '000000'){
	       			vm.user = r.user;
	       			vm.user.tel = parseInt(vm.user.tel);
	       		}else{
	       		 pb.alertErr('获取用户信息失败');
	       		}
	       },function(err){
	    	   pb.alertErr('获取用户信息失败');
	       });
       },
       getTopicList(){
    	   var url = "./listTopic.action";
    	   var params = {
    			   //"pagemap.pname":"招领模块",
       			   "pagemap.page":vm.currPage,
                   "pagemap.size":vm.pageSize,
                   "pagemap.userCode":window.parent.currUserCode,
                   //"pagemap.tname":vm.tname,
                   //"pagemap.date":vm.createDate
    	   }
    	   pb.ajaxPost(url,params,function(r){
    		    if(r.code == '000000'){
	       			vm.tableData = r.topicList;
	       			var nowDate = new Date().getTime();
	       			if(vm.tableData !== null && vm.tableData.length>0){
	       				for(var i=0;i<vm.tableData.length;i++){
	       					var tempDate = new Date(vm.tableData[i].t_create_time).getTime();
	       					vm.tableData[i].flag = nowDate - tempDate > (1 * 3600 * 1000 * 24)?0:1;
	       					
	       					vm.userEditorEdit.setContent(vm.tableData[i].tcontent);
	       					vm.tableData[i].tcontent = vm.userEditorEdit.getContentTxt();
		       			}
	       			}
	       			vm.userEditorEdit.setContent('');
	       			vm.total = r.topiccount;
	       		}else{
               		pb.alertErr("加载失败");
               	}
	       },function(err){
	    	   pb.alertErr("加载失败");
	       });
       },
       open(tid){
    	   /*$('#content').empty();
    	   vm.topicV = true;
    	   vm.getRecord(tid);*/
    	   window.parent.vm.goTo('viewTopic',tid);
       },
       getRecord(tid){
    	   var url = "./getTopicById.action";
    	   var params = {
    		   'topic.tid':tid
    	   }
    	   pb.ajaxPost(url,params,function(r){
	       		if(r.code == '000000'){
	       			vm.topicData = r.topic;
	       			vm.userEditorEdit.setContent(vm.topicData.tcontent);
	       			vm.topicData.createDate = moment(vm.topicData.t_create_time).format('YYYY-MM-DD');
	       			vm.topicData.tcontent = vm.userEditorEdit.getContent();
	       			$('#content').append(vm.topicData.tcontent);
	       			vm.userTopic = r.topic;
	       		}else{
	       		 pb.alertErr('获取信息失败');
	       		}
	       },function(err){
	    	   pb.alertErr('获取信息失败');
	       });
       },
       submit:function(formName){
    	   vm.$refs[formName].validate(function(valid) {
 	          if (valid) {
	                  var url = './updateUser.action';
	                  var params = {
	                		"user.id":window.parent.currUserId,
	                  		"user.userCode":vm.user.userCode,
	                  		"user.userName":vm.user.userName,
	                  		"user.password":vm.user.password,
	                  		"user.tel":vm.user.tel,
	                  		"user.email":vm.user.email
	                  		
	                  };          
	                  pb.ajaxPost(url,params,function(r){
	                	
	                  	if(r.code == '000000'){
	                  		vm.back();
	                  	}else{
	                  		
	                  		pb.alertErr("修改失败");
	                  	}
	                  	},function(err){
	                  		
	                  		pb.alertErr("修改超时");
	                  	});
 	          } else {
 	            return false;
 	          }
	 	  });
       },
       submit2:function(formName){
    	   vm.$refs[formName].validate(function(valid) {
 	          if (valid) {
                  var url ="";
 	        	  if(vm.userTopic.tid == null || vm.userTopic.tid == ''){
 	        		 url = './addTopic.action';
 	        	  }else{
 	        		 url = './updateTopic.action';
 	        	  }
                  var params = {
                		"topic.tid":vm.userTopic.tid,
                  		"topic.tname":vm.userTopic.tname,
                  		"topic.tcontent":vm.userEditorEdit.getContent()
                  };
                  
                  pb.ajaxPost(url,params,function(r){
                  	if(r.code == '000000'){
                  		pb.alertSuc("提交成功");
                  		vm.back2('userTopicRef');
                  	}else{
                  		pb.alertErr("提交失败");
                  	}
                  	},function(err){

                  		pb.alertErr("操作超时");
                  	});
 	          } else {
 	            return false;
 	          }
 	    });
       },
       end:function(tid){
    	   var params={
    		   'topic.tid':tid
    	   }
    	   pb.ajaxPost('./endTopic.action',params,function(r){
             	if(r.code == '000000'){
             		pb.alertSuc("提交成功");
             		vm.getTopicList();
             	}else{
             		pb.alertErr("提交失败");
             	}
             	},function(err){

             		pb.alertErr("操作超时");
             	});
       },
       //删除
       del:function(tid){
    	   /*if(vm.multipleSelection == null || vm.multipleSelection.length <=0){
    		   pb.alertWar("请选择至少一条记录");
    		   return false;
    	   }
    	   
    	   for(var i=0;i<vm.multipleSelection.length;i++){
    		   tids.push(vm.multipleSelection[i].id);
    	   }*/
    	   var ids = [];
    	   ids.push(tid);
    	   vm.$confirm('确定删除么?','提示',{
               confirmButtonText: '确定',
               cancelButtonText: '取消',
               type:'warning',
               callback:function(e){
                   if(e === 'confirm'){
                	   var params = {
                		   "res.ids":JSON.stringify(ids)
                	   }
                       pb.ajaxPost("./delTopic.action",params,function(res){
                           if(res.code == '000000'){
                        	   pb.alertSuc('删除成功');
                               vm.getTopicList();
                           }else{
                           	pb.alertErr('删除失败');
                           }
                       });
                   	
                   }
               }
           });
       },
       back:function(formName){
    	   vm.$refs[formName].resetFields();
    	   vm.user={};
    	   vm.loadData();
    	   vm.pageA = true;
    	   vm.pageB = false;
    	   vm.pageC = false;
       },
       back2:function(formName){
    	   vm.$refs[formName].resetFields();
    	   vm.user={};
    	   vm.loadData();
    	   vm.pageA = true;
    	   vm.pageB = false;
    	   vm.pageC = false;
       },
       //样式控制
       getCss:function(){
       	
       	vm.bodyH = document.body.clientHeight;
       	vm.bodyW = document.body.clientWidth;
      
       	//$('#left').css("height",vm.bodyH - 100 - 20 - 10);
       	//$('#right').css("height",vm.bodyH -100 -20 - 10);

       },
       loadEdit:function(){
    	   vm.userEditorEdit = UM.getEditor("userEditorEdit");
    	   $('.edui-container').css("width","");
    	   $('.edui-container').css("height","600px");
    	   $('.edui-body-container').css("width","");
       }


    }
    //vue实例 的methods结束
});
vm.getCss();
vm.loadData();
vm.loadEdit();